from fastapi import APIRouter, Depends, Body
from dependencies import get_mongo_settings_coll, require_admin
import motor.motor_asyncio

from enums import DriveActionSettingsName
from schema.req.drive_action_settings import MapInfoSettings, RenterMarkSettings, UserFeedbackSettings, ViolationShowSettings, ViolationRankSettings, ComplaintTendency, ComplaintShow
from schema.resp import RestfulModel
from schema.resp.drive_action_settings import ComplaintDealState, Settings
from typing import List
from utils import update_settings


AsyncIOMotorCollection = motor.motor_asyncio.AsyncIOMotorCollection


drive_behavior_settings_router = APIRouter(prefix='/driver-behavior/setting',
                                           dependencies=[Depends(require_admin), ],
                                           tags=['驾驶行为感知-配置项'])

success_resp = RestfulModel.success(None)

settings_name_map = {
    'drive_action_map_info': '驾驶行为-地图信息',
    'drive_action_renter_mark': '驾驶行为-用户评价',
    'drive_action_renter_complaint': '驾驶行为-用户投诉',
    'drive_action_compliant_tendency': '驾驶行为-投诉趋势',
    'drive_action_rule_violation': '驾驶行为-违章信息展示',
    'drive_action_violation_rank': '驾驶行为-违章信息排名',
    'drive_action_user_feedback': '驾驶行为-异常驾驶行为',
    'business_safety_interface_statistic': '业务安全-业务数据统计',
    'log-file': '用于解析业务安全数据的日志文件路径',
    'business_safety_interface_call_freq_statistic': '业务安全-接口调用频率',
    'business_safety_key_business_statistic': '业务安全-关键业务统计'
}


@drive_behavior_settings_router.get('/init-settings',
                                    response_model=RestfulModel[dict],
                                    summary='读取用于初始化的配置')
async def read_init_settings(coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    cursor = coll.find()
    settings_list = await cursor.to_list(None)
    result = {
        settings['name']: {'zh_name': settings_name_map.get(settings['name']), 'data': settings['data']}
        for settings in settings_list
    }
    return RestfulModel.success(result)


@drive_behavior_settings_router.post('/map-info',
                                     response_model=RestfulModel,
                                     summary='修改地图信息的配置')
async def modify_map_info_settings(settings: MapInfoSettings = Body(...),
                                   coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.MAP_INFO, settings.dict())
    return success_resp


@drive_behavior_settings_router.post('/renter-mark',
                                     response_model=RestfulModel,
                                     summary='修改用户评价表的配置')
async def modify_renter_mark_settings(settings: RenterMarkSettings = Body(...),
                                      coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.RENTER_MARK, settings.dict())
    return success_resp


@drive_behavior_settings_router.post('/user-feedback',
                                     response_model=RestfulModel,
                                     summary='修改异常驾驶的配置')
async def modify_user_feedback_settings(settings: UserFeedbackSettings = Body(...),
                                        coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.USER_FEEDBACK, settings.dict())
    return success_resp


@drive_behavior_settings_router.get('/violation-types',
                                    response_model=RestfulModel[List],
                                    summary='获取违章的类型，用于供前端选择类型')
async def get_violation_types():
    return RestfulModel.success(['闯红灯', '违规停放', '违法行为'])


@drive_behavior_settings_router.post('/violation-show',
                                     response_model=RestfulModel,
                                     summary='修改违章信息显式的配置')
async def modify_violation_show_settings(settings: ViolationShowSettings = Body(...),
                                         coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.VIOLATION_SHOW, settings.dict())
    return success_resp


@drive_behavior_settings_router.post('/violation-rank',
                                     response_model=RestfulModel,
                                     summary='修改违章排名的配置')
async def modify_violation_rank_settings(settings: ViolationRankSettings = Body(...),
                                         coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.VIOLATION_RANK, settings.dict())
    return success_resp


@drive_behavior_settings_router.get('/complaint-deal-states',
                                    response_model=RestfulModel[List[ComplaintDealState]],
                                    summary='获取违章处理的所有状态，以供用户在前端选择')
def get_complaint_deal_states():
    state_tuples = [(0, '待处理'), (1, '处理中'), (2, '已处理'), (3, '处理失败')]
    states = [ComplaintDealState(key=key, label=label) for key, label in state_tuples]
    return RestfulModel.success(states)


@drive_behavior_settings_router.post('/complaint-tendency',
                                     response_model=RestfulModel,
                                     summary='修改用户投诉趋势的配置')
async def modify_complaint_tendency_settings(settings: ComplaintTendency = Body(...),
                                             coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.COMPLAINT_TENDENCY, settings.dict())
    return success_resp


@drive_behavior_settings_router.post('/complaint-show',
                                     response_model=RestfulModel,
                                     summary='修改用户投诉信息显示的配置')
async def modify_complaint_show_settings(settings: ComplaintShow = Body(...),
                                         coll: AsyncIOMotorCollection = Depends(get_mongo_settings_coll)):
    await update_settings(coll, DriveActionSettingsName.COMPLAINT_SHOW, settings.dict())
    return success_resp
